package com.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.common.util.DateHelper;

@SuppressWarnings("serial")
public class DateTag extends TagSupport {

	private Object date;
	private String format = "yyyy-MM-dd HH:mm:ss";

	public int doStartTag() throws JspException {
		if (date == null)
			return SKIP_BODY;
		JspWriter out = pageContext.getOut();
		String dateStr = "";
		if (date instanceof java.sql.Timestamp)
			dateStr = DateHelper.getDateStr((java.sql.Timestamp) date, format);
		else if (date instanceof java.sql.Date)
			dateStr = DateHelper.getDateStr((java.sql.Date) date, format);
		else if (date instanceof java.util.Date)
			dateStr = DateHelper.getDateStr(DateHelper.utilDateToSqlDate((java.util.Date) date), format);
		else if (date instanceof java.sql.Time)
			dateStr = DateHelper.getDateStr(new java.sql.Date(((java.sql.Time) date).getTime()), format);
		else if (date instanceof Long)
			dateStr = DateHelper.getDateStr(new java.sql.Date((Long) date), format);
		try {
			out.print(dateStr);
			out.flush();
		} catch (Exception e) {
		}
		return SKIP_BODY;
	}

	public Object getDate() {
		return date;
	}

	public void setDate(Object date) {
		this.date = date;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

}
